package gui;
import java.awt.BorderLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.AbstractTableModel;

import core.Main;
import dataStructures.QueryAndResults;

public class QueryTable extends JTable {
	private static final long serialVersionUID = 1L;
	AbstractTableModel tableModel;
	JTable table;
	MyMainFrame frame;
	
	
	public QueryTable(final MyMainFrame frame) {
		this.frame = frame;
		setLayout(new BorderLayout());
		
		table = new JTable();
		table.getTableHeader().setReorderingAllowed(false);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		
		tableModel = new QueryTableModel();
		table.setModel(tableModel);
		
		table.addMouseListener(new MouseListener() {
			public void mouseClicked(MouseEvent arg0) {
				System.out.println(table.getSelectedRow());
				Main.rowSelected = table.getSelectedRow();
				frame.updateTweetsTable();
			}
			public void mouseEntered(MouseEvent e) {}
			public void mouseExited(MouseEvent e) {}
			public void mousePressed(MouseEvent e) {}
			public void mouseReleased(MouseEvent e) {}
		});

	
		add(new JScrollPane(table), BorderLayout.CENTER);
	}
}

//TODO: columns sorting - http://www.exampledepot.com/egs/javax.swing.table/SortCol.html

class QueryTableModel extends AbstractTableModel {
	private static final long serialVersionUID = 1L;
	private final String[] colNames = {
		"time",
    	"term",
		};

	public boolean isCellEditable(int row, int col) {
		return false;
    }
	
	public int getColumnCount() { return colNames.length; }
	public int getRowCount() { return Main.data.size(); }
	public String getColumnName(int col) { return colNames[col]; }
	public Object getValueAt(int row, int col) {
		QueryAndResults queryAndResult = Main.data.get(row);
		switch (col) {
		case 0: return queryAndResult.date;
		case 1: return queryAndResult.query.term;
		default : return null;
		}
	}

//	public void setValueAt(Object value, int row, int col) {
//		Aplikacja.dane.set(row, Aplikacja.dane.get(row).set(value, col)); //pobieramy, zmieniamy, ustawiamy
//		fireTableCellUpdated(row, col);
//	}
	
//	public void addRecord(Osoba o) {
//		if (o == null) return;
//		Aplikacja.dane.add(o);
//		int row = Aplikacja.dane.indexOf(o);
//		fireTableRowsInserted(row, row);
//	}
}
